//
//  KYCalendarView.m
//  KuaiyanCalendar
//
//  Created by 罗新 on 2017/10/17.
//  Copyright © 2017年 KuaiYan  Co.,LTD. All rights reserved.
//

#import "KYCalendarView.h"
#import "NSDate+KY.h"
#import "UIView+KY.h"

#define ky_signInMargin 10
#define ky_screenWidth [UIScreen mainScreen].bounds.size.width
#define ky_screenHeight [UIScreen mainScreen].bounds.size.height
@interface KYCalendarView()
/**
 头部View
 */
@property (nonatomic, weak) UIView *headView;

/**
 内容View
 */
@property (nonatomic, weak) UIView *contentView;

/**
 关闭按钮
 */
@property (nonatomic, weak) UIButton *closeButton;

/**
 时间数据
 */
@property (nonatomic, strong) NSMutableArray *dayArrM;

@end
@implementation KYCalendarView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpSubviews];
    }
    return self;
}

- (void)setUpSubviews{
    //0.头部背景view
    UIView *headbgView = [[UIView alloc] init];
    headbgView.frame = CGRectMake(0, 0, ky_screenWidth, 75);
    headbgView.backgroundColor = [UIColor clearColor];
    [self addSubview:headbgView];
    self.headView = headbgView;
    
    //1.左边按钮(上一月)
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(0,0,40,40);
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 18)];
    [leftButton setImage:[UIImage ky_imageNamed:@"sign_in_last_page"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:leftButton];
    
    //2.右边按钮(下一月)
    UIButton *rightButton = [[UIButton alloc] init];
    rightButton.frame = CGRectMake(ky_screenWidth - 40,0, 40, 40);
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(10, 18, 10, 10)];
    [rightButton setImage:[UIImage ky_imageNamed:@"sign_in_next_page"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:rightButton];
    //3.中间月份Label
    UILabel *calendarLabel = [[UILabel alloc] init];
    calendarLabel.text = @"9999年99月";
    calendarLabel.font = [UIFont systemFontOfSize:16];
    calendarLabel.textColor = [UIColor whiteColor];
    [calendarLabel sizeToFit];
    calendarLabel.center = self.headView.center;
    calendarLabel.ky_centerY -= 15;
    calendarLabel.ky_centerX += 20;
    [self.headView addSubview:calendarLabel];
    self.calendarLabel = calendarLabel;
    
    //4.中间日历图标
    UIImageView *calendarIconImageV = [[UIImageView alloc] init];
    calendarIconImageV.frame = CGRectMake(100,0,40,40);
    calendarIconImageV.image = [UIImage ky_imageNamed:@"calendar_icon"];
    calendarIconImageV.ky_right = calendarLabel.ky_left;
    [self.headView addSubview:calendarIconImageV];
    
    //5.星期栏view
    UIView *weekView = [[UIView alloc] init];
    weekView.frame = CGRectMake(ky_signInMargin,rightButton.ky_height, ky_screenWidth - 2 * ky_signInMargin,self.headView.ky_height - rightButton.ky_height);
    weekView.backgroundColor = [UIColor colorWithRed:232/255. green:131/255. blue:128/255. alpha:1];
    NSArray *weekArr = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
    CGFloat weekWidth = weekView.ky_width / weekArr.count;
    for (int i = 0; i< weekArr.count; i++) {
        UIView *weekV = [[UIView alloc] init];
        weekV.backgroundColor = [UIColor clearColor];
        weekV.frame = CGRectMake(i * weekWidth,0, weekWidth, weekView.ky_height);
        weekV.layer.borderWidth = 0.15;
        weekV.layer.borderColor = [UIColor lightGrayColor].CGColor;
        UILabel *label = [[UILabel alloc] initWithFrame:weekV.bounds];
        label.text = weekArr[i];
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor brownColor];
        label.textAlignment=NSTextAlignmentCenter;
        [weekV addSubview:label];
        [weekView addSubview:weekV];
    }
    [self.headView addSubview:weekView];
}
- (void)leftButtonClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(calendarHeaderNavButtonClick:)]) {
        [self.delegate calendarHeaderNavButtonClick:-1];
    }
}
- (void)rightButtonClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(calendarHeaderNavButtonClick:)]) {
        [self.delegate calendarHeaderNavButtonClick:+1];
    }
}
@end
